package controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import data.CooperationData;
import data.GeoData;
import data.PageData;
import forms.CooperationForm;

import models.CooperationModel;
import models.GeoModel;
import models.PageModel;


public class CooperationServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String s = (String) request.getParameter("s");
		if (s == null) {
			s = "cooperation";
		}
		
		TreeMap check = new TreeMap();
		
		GeoData geoData = new GeoData();
		ArrayList<GeoModel> geos = geoData.getCities();
		
		CooperationModel cooperation = new CooperationModel();
		
		PageData pageData = new PageData();
		PageModel businessCooperation = pageData.getBusinessCooperation();
		
		request.setAttribute("businessCooperation", businessCooperation);
		request.setAttribute("s", s);
		request.setAttribute("geos", geos);
		request.setAttribute("cooperation", cooperation);
		request.setAttribute("check", check);
		try {
			request.getRequestDispatcher("/pages/cooperation.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		String s = (String) request.getParameter("s");
		if (s == null) {
			s = "cooperation";
		}
		
		CooperationForm cooperationForm = new CooperationForm();
		CooperationModel cooperation = new CooperationModel();
		TreeMap form = cooperationForm.checkValid(request, cooperation);
		TreeMap check = (TreeMap) form.get("checkValid");
		cooperation = (CooperationModel) form.get("cooperation");
		
		GeoData geoData = new GeoData();
		ArrayList<GeoModel> geos = geoData.getCities();
		
		PageData pageData = new PageData();
		PageModel businessCooperation = pageData.getBusinessCooperation();
		
		if (check.size() == 0) {
			cooperation.setId(IdUniqueHelper.getId());
			CooperationData cooperationData = new CooperationData();
			cooperationData.insert(cooperation);
			cooperation = new CooperationModel();
			session.setAttribute("success", "Cảm ơn bạn đã liên hệ hợp tác với chúng tôi. Chúng tôi sẽ liên lạc lại với bạn trong thời gian sớm nhất. ");
		}
		
		request.setAttribute("businessCooperation", businessCooperation);
		request.setAttribute("s", s);
		request.setAttribute("geos", geos);
		request.setAttribute("cooperation", cooperation);
		request.setAttribute("check", check);
		try {
			request.getRequestDispatcher("/pages/cooperation.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
}
